package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.CollectAddOrDeleteBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.GoodsListVO;
import com.cskaoyan.service.CollectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信小程序-个人中心-收藏，需要登录！！！
 * 创建日期: 2022/04/09 19:50
 *
 * @author ChengWenqi
 */

@RestController
@RequestMapping("wx/collect")
public class CollectController {

    @Autowired
    CollectService collectService;

    // 获取当前用户的所有收藏
    // 无法抓包，不知道需要响应什么参数，假设响应商品list（加一个valueId） 👉 猜对了
    @RequestMapping("list")
    public BaseRespVo collectList(byte type, int page, int limit) {
        // 获取登录的用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        Integer userId = user.getId();
        GoodsListVO goodsListVO = collectService.queryCollectList(type, page, limit, userId);
        return BaseRespVo.ok(goodsListVO);
    }

    // 添加/取消收藏
    @RequestMapping("addordelete")
    public BaseRespVo collectAddOrDelete(@RequestBody CollectAddOrDeleteBO collectAddOrDeleteBO) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        collectService.collectAddOrDelete(collectAddOrDeleteBO, user);
        return BaseRespVo.ok();
    }

}
